<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10 paddingBottom20">
      <div class="main_crumbs boxdq">
        <div class="main_crumbs_l">
          <a href="javascript:;" @click="$router.back(-1)">
            <i class="el-icon-arrow-down"></i>返回
          </a>
          <em>|</em>
          <span>批发差额</span>
        </div>
      </div>
      <div class="mainor_form marginTop15">
        <div class="mainor_te"></div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="批发商" prop="dispatch_name">
            <el-input
              v-model.trim="form.dispatch_name"
              placeholder="批发商"
              clearable
              class="wid220"
            ></el-input>
          </el-form-item>
          <el-form-item label="流水日期" prop="createTime">
            <el-date-picker
              v-model="createTime"
              type="daterange"
              class="time260"
              :editable="false"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :clearable="true"
              :picker-options="pickerOptions"
            />
          </el-form-item>
          <el-form-item label="流水类型" prop="type">
            <el-select
              v-model.trim="form.type"
              placeholder="流水类型"
              class="wid220"
            >
              <el-option label="所有" :value="parseInt(0)"></el-option>
              <el-option
                v-for="(group, index) in type_arr"
                :key="index"
                :label="group"
                :value="parseInt(index) + 1"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- <div class="more el-form-item" @click="more">
            <span>更多</span>
            <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
          </div> -->
          <div class="clearfix"></div>
          <el-collapse-transition>
            <div v-if="moredata">
              <div class="demo">
                <!-- <el-form-item label="状态" prop="status">
                  <el-select
                    v-model.trim="form.status"
                    placeholder="状态"
                    class="wid220"
                  >
                    <el-option label="所有" :value="parseInt(0)"></el-option>
                    <el-option
                      v-for="(group, index) in type_arr"
                      :key="index"
                      :label="group"
                      :value="parseInt(index) + 1"
                    ></el-option>
                  </el-select>
                </el-form-item> -->
              </div>
            </div>
          </el-collapse-transition>
          <el-form-item>
            <el-button class="reset marginLeft10" type="primary" @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="main_table" style="min-height: 570px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
        >
          <!-- :header-cell-class-name="this.$verticaline" show-summary
          :summary-method="getSummaries" -->
          <!-- <el-table-column type="selection" /> -->

          <el-table-column label="批发商" prop="" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{
                scope.row.dispatch_name
                  ? scope.row.dispatch_name.company_name
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="关联订单"
            prop="order_sn"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="流水类型"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ type_arr[scope.row.type] }}
            </template>
          </el-table-column>
          <el-table-column
            label="金额"
            prop="balance_left"
            :show-overflow-tooltip="true"
          />
          <!-- <el-table-column label="状态" prop="" :show-overflow-tooltip="true" /> -->
          <el-table-column
            label="变动后差额"
            prop="balance_change"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="流水日期"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <div>
                {{
                  scope.row.updated_at
                    ? parseTime(scope.row.updated_at, "{y}-{m}-{d}")
                    : ""
                }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_bulk_balance_warter } from "@/api/bulk/bulkBalance";

import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";
import dateOption from "@/options/dateOption";

export default {
  data() {
    return {
      parseTime,
      createTime: [],
      pickerOptions: dateOption,
      // 搜索
      form: {
        dispatch_name: "", //批发商
        start_time: "",
        end_time: "",
        type: "", //流水类型
      },
      type_arr: [],
      // 更多
      moredata: false,
      // 列表数据
      list: [],
      listLoading: false,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,

      // 合计数据
      total: {
        order_money: 0,
        diff_money: 0,
        account_money: 0,
      },
    };
  },
  methods: {
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.get_bulk_balance_warter();
    },
    changePage(page) {
      this.current.page = page;
      this.get_bulk_balance_warter();
    },

    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.get_bulk_balance_warter();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.createTime = [];
      this.get_bulk_balance_warter();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    get_bulk_balance_warter() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      get_bulk_balance_warter(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
          this.type_arr = res.data.type_arr;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime ? this.createTime[0] : "";
      form.end_time = this.createTime ? this.createTime[1] : "";
      return form;
    },

    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 1:
            sums[index] = "合计";
            break;
          case 5:
            sums[index] = this.$toFixed(this.total.order_money);
            break;
          case 6:
            sums[index] = this.$toFixed(this.total.account_money);
            break;
          case 7:
            sums[index] = this.$toFixed(this.total.diff_money);
            break;
        }
      });

      return sums;
    },
  },
  created() {
    let dispatch_name = this.$route.query.dispatch_name;
    if (dispatch_name) {
      this.form.dispatch_name = dispatch_name;
    }
    this.get_bulk_balance_warter();
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.mainor_form form {
  width: calc(100% - 280px);
}
</style>
